/*
 * @Author: Leo.wang wanglizhigs@163.com
 * @Date: 2023-12-05 23:38:21
 * @LastEditors: Leo.wang wanglizhigs@163.com
 * @LastEditTime: 2024-01-04 21:43:32
 * @FilePath: /ruoyi-ui/mock/common.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const Mock = require('mockjs');
class Paginator {
    constructor(total, pageCount, pageSize) {
        pageCount = parseInt(pageCount);
        pageSize = parseInt(pageSize);
        this.total = total || 0;
        this.pageCount = pageCount;
        this.pageSize = pageSize;
        this.currentPage = 1;
        // this.totalPages = total%pageSize == 0 ? total/pageSize : total/pageSize + 1;
    }
}

function paramToObject(url) {
    const search = url.split('?')[1]
    if (!search) {
        return {}
    }
    return JSON.parse('{"' + decodeURIComponent(search).replace(/"/g, '\\"').replace(/&/g, '","').replace(/=/g, '":"') + '"}')
}

function paramToString(url) {
    const search = url.split('/').reverse();
    return search[0];
}



function initData(data,count) {
    let list = [];
    ((count)=>{
        for(let i = 0; i < count; i++){
            list.push({
              id: i+1,
              ...data
            });
        }
    })(count);
    return list;
}

function pagination(list,currentPage,size){
    currentPage = parseInt(currentPage);
    size = parseInt(size);
    return list.filter((item, index) => index < size * currentPage && index >= size * (currentPage - 1));
}

function response(result=null,code=200,msg='ok'){
    return {
        code: code,
        msg: msg,
        result: result,
    }

}

module.exports = {
  Paginator,
  paramToObject,
  paramToString,
  initData,
  pagination,
  response
}
